package com.example.feiniaoji;


import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.content.ContextCompat;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.SearchView;
import android.widget.TextView;

import com.baidu.location.BDLocation;
import com.baidu.location.BDLocationListener;
import com.baidu.location.LocationClient;
import com.baidu.location.LocationClientOption;

import org.w3c.dom.Text;

import java.util.ArrayList;
import java.util.List;

import function.stragetyAdapter;
import function.strategy;

import static android.content.ContentValues.TAG;


/**
 * A simple {@link Fragment} subclass.
 */
public class first extends Fragment {
    private String[] mStrs = {"aaa", "bbb", "ccc", "airsaid"};
    private List<strategy> strategyList = new ArrayList<>();
    private stragetyAdapter adapter;
    private ListView list;
    public TextView place = null;
    public LocationClient mLocationClient = null;
    public MyLocationListenner myListener = new MyLocationListenner();

    public first() {
        // Required empty public constructor
    }



    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        mLocationClient = new LocationClient(getActivity());
        mLocationClient.registerLocationListener(myListener);
        View view = inflater.inflate(R.layout.fragment_first,container,false);
        SearchView citySearch = (SearchView)view.findViewById(R.id.searchView);
        final ListView cityList = (ListView)view.findViewById(R.id.listView);
        cityList.setAdapter(new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1, mStrs));
        cityList.setTextFilterEnabled(true);
        list = view.findViewById(R.id.stragetylist);
        initLocation();
        Button btnHotel = view.findViewById(R.id.HotelBtn);
        Button btnView = view.findViewById(R.id.ViewBtn);
        Button btnEat = view.findViewById(R.id.EatBtn);
        Button btnShop = view.findViewById(R.id.ShopBtn);
        place = view.findViewById(R.id.location);
        mLocationClient.start();
        citySearch.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
            @Override
            public boolean onQueryTextSubmit(String query) {
                return false;
            }
            @Override
            public boolean onQueryTextChange(String newText) {
                if (!TextUtils.isEmpty(newText)){
                    cityList.setFilterText(newText);
                }else{
                    cityList.clearTextFilter();
                }
                return false;
            }
        });
        btnHotel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent();
                intent.setClass(getActivity(),HotelActivity.class);
                startActivity(intent);
            }
        });
        btnView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent();
                intent.setClass(getActivity(),ViewActivity.class);
                startActivity(intent);
            }
        });
        btnEat.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent();
                intent.setClass(getActivity(),ViewActivity.class);
                startActivity(intent);
            }
        });
        btnShop.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent();
                intent.setClass(getActivity(),PlayActivity.class);
                startActivity(intent);
            }
        });
        initStrategys();
        adapter = new stragetyAdapter(getActivity(),R.layout.strategy_item,strategyList);
        list.setAdapter(adapter);
        return view;
    }

    private void initStrategys() {
        for(int i = 0; i<10; i++){
            strategy first = new strategy("http://pic39.nipic.com/20140226/18071023_164300608000_2.jpg","first","Lin");
            strategyList.add(first);
            strategy second = new strategy("http://pic39.nipic.com/20140226/18071023_164300608000_2.jpg","second","Jack");
            strategyList.add(second);
        }
    }

    public class MyLocationListenner implements BDLocationListener {
        @Override
        public void onReceiveLocation(BDLocation location) {
            String locate = location.getAddrStr();
            place.setText(location.getCity());
        }
        @Override
        public void onConnectHotSpotMessage(String x,int y){}
    }

    private void initLocation(){
        LocationClientOption option = new LocationClientOption();
        option.setLocationMode(LocationClientOption.LocationMode.Hight_Accuracy);
        //可选，默认高精度，设置定位模式，高精度，低功耗，仅设备

        option.setCoorType("bd09ll");
        //可选，默认gcj02，设置返回的定位结果坐标系

        int span=1000;
        option.setScanSpan(span);
        //可选，默认0，即仅定位一次，设置发起定位请求的间隔需要大于等于1000ms才是有效的

        option.setIsNeedAddress(true);
        //可选，设置是否需要地址信息，默认不需要

        option.setOpenGps(true);
        //可选，默认false,设置是否使用gps

        option.setLocationNotify(true);
        //可选，默认false，设置是否当GPS有效时按照1S/1次频率输出GPS结果

        option.setIsNeedLocationDescribe(true);
        //可选，默认false，设置是否需要位置语义化结果，可以在BDLocation.getLocationDescribe里得到，结果类似于“在北京天安门附近”

        option.setIsNeedLocationPoiList(true);
        //可选，默认false，设置是否需要POI结果，可以在BDLocation.getPoiList里得到

        option.setIgnoreKillProcess(false);
        //可选，默认true，定位SDK内部是一个SERVICE，并放到了独立进程，设置是否在stop的时候杀死这个进程，默认不杀死

        option.SetIgnoreCacheException(false);
        //可选，默认false，设置是否收集CRASH信息，默认收集

        option.setEnableSimulateGps(false);
        //可选，默认false，设置是否需要过滤GPS仿真结果，默认需要

        mLocationClient.setLocOption(option);
    }
}
